package com.example.concurrent.chapter03;

import java.util.concurrent.TimeUnit;

/**
 * @author Create By Liuqz
 * @date 2018/11/20 16:17
 */
public class InterruptThreadExit {


    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(){
           /* @Override
            public void run() {
                System.out.println("I will start work");
                while (!isInterrupted()) {
                    //working
                }
                System.out.println("I will be exiting");
            }*/

            @Override
            public void run() {
                System.out.println("I will start work");
//                for (; ; ) {
//                    //working
//                    try {
//                        TimeUnit.MILLISECONDS.sleep(1);
//                    } catch (InterruptedException e) {
//                        break;
//                    }
//                }
                while (!interrupted()) {
                    //working
                }
                System.out.println("I will be exiting");
            }
        };

        t.start();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("System will be shutdown");
        //调用 Thread.sleep 线程则进入阻塞状态, 调用interrupt() 则打断阻塞
        t.interrupt();
    }
}
